package com.nuoniu.sibanyun.service.work.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.exception.NuoNiuException;
import com.nuoniu.sibanyun.common.lang.StringUtils;
import com.nuoniu.sibanyun.entity.work.WorkPlanSummary;
import com.nuoniu.sibanyun.mapper.work.WorkPlanSummaryMapper;
import com.nuoniu.sibanyun.service.work.IWorkPlanSummaryService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 工作计划总结 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-08-19
 */
@Service
public class WorkPlanSummaryServiceImpl extends ServiceImpl<WorkPlanSummaryMapper, WorkPlanSummary> implements IWorkPlanSummaryService {

    @Override
    public Page<WorkPlanSummary> pageList(Page pages, WorkPlanSummary planSummary) {
        UserInfo userInfo = UserThreadLocal.get();
        planSummary.setCompanyId(userInfo.getCompanyId());
        if(planSummary.getTabKey() == 1) {
            planSummary.setCreateUserId(userInfo.getUserId());
        }else if(planSummary.getTabKey() == 2) {
            planSummary.setSubmitId(userInfo.getUserId());
        }else if (planSummary.getTabKey() == 3){
            planSummary.setCcId(userInfo.getUserId().toString());
        }
        return baseMapper.pageList(pages, planSummary);
    }

    @Override
    public void returnPlan(WorkPlanSummary workPlanSummary) {
        WorkPlanSummary planSummary = this.getById(workPlanSummary.getId());
        if(planSummary == null || planSummary.getReadStatus().equals(1) || planSummary.getFinishStatus()) {
            throw new NuoNiuException("已被报送人查看,不能撤回");
        }

        baseMapper.returnPlan(workPlanSummary);
    }

    @Override
    public void finish(List<String> ids) {
        for(String id : ids) {
            WorkPlanSummary planSummary = this.getById(id);
            if(planSummary.getStatus().equals(0)) {
                throw new NuoNiuException("未提交的不能参与存档");
            }

            planSummary.setFinishStatus(true);
            this.updateById(planSummary);
        }
    }
}
